package cn.hnucm.controller;

import cn.hnucm.utils.IPUtils;
import cn.hnucm.utils.Result;
import cn.hnucm.vo.ReturnOrderInfo;
import com.alibaba.nacos.common.utils.IPUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;
import cn.hnucm.service.CenterControllerService;
import cn.hnucm.service.EsControllerService;
import cn.hnucm.service.HbControllerService;
import cn.hnucm.vo.Goods;

import javax.annotation.Resource;
import javax.servlet.http.HttpServletRequest;

/**
 * @Author:liweihua
 * @Description:
 * @Date: 21:32$ 2021/12/28/028$
 * @Modified:
 */
@RestController
@RequestMapping("servicecenter")
public class CenterController {
    @Resource(name = "esControllerService")
    CenterControllerService centerControllerService;

    @Resource(name = "hbControllerService")
    CenterControllerService hbCenterControllerService;

    @RequestMapping("addesorder")
    public Result addEsOrder(@RequestBody Goods goods, HttpServletRequest request){
        String ipAddr = IPUtils.getIpAddr(request);
        ReturnOrderInfo purchase = centerControllerService.purchase(goods,ipAddr);
        return Result.ok(purchase);
    }

    @RequestMapping("confirmesorder/{orderId}")
    public Result confirmEsOrder(@PathVariable int orderId, HttpServletRequest request){
        String ipAddr = IPUtils.getIpAddr(request);
        ReturnOrderInfo confirm = centerControllerService.confirm(orderId,ipAddr);
        return Result.ok(confirm);
    }

    @RequestMapping("unconfirmesorder/{orderId}")
    public Result unConfirmEsOrder(@PathVariable int orderId, HttpServletRequest request){
        String ipAddr = IPUtils.getIpAddr(request);
        int i = centerControllerService.unConfirm(orderId,ipAddr);
        if (i ==1 )
            return Result.ok("撤销成功");
        else
            return Result.error("撤销失败");
    }

    @RequestMapping("addhborder")
    public Result addHbOrder(@RequestBody Goods goods, HttpServletRequest request){
        String ipAddr = IPUtils.getIpAddr(request);
        ReturnOrderInfo purchase = hbCenterControllerService.purchase(goods,ipAddr);
        return Result.ok(purchase);
    }

    @RequestMapping("confirmhborder/{orderId}")
    public Result confirmHbOrder(@PathVariable int orderId, HttpServletRequest request){
        String ipAddr = IPUtils.getIpAddr(request);
        ReturnOrderInfo confirm = hbCenterControllerService.confirm(orderId,ipAddr);
        return Result.ok(confirm);
    }

    @RequestMapping("unconfirmhborder/{orderId}")
    public Result unConfirmHbOrder(@PathVariable int orderId, HttpServletRequest request){
        String ipAddr = IPUtils.getIpAddr(request);
        int i = hbCenterControllerService.unConfirm(orderId,ipAddr);
        if (i ==1 )
            return Result.ok("撤销成功");
        else
            return Result.error("撤销失败");
    }
}
